package com.niit.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.niit.model.Book;
import com.niit.service.BookService;
import com.niit.service.impl.BookServiceImpl;

/**
 * Servlet implementation class QueryBooksServlet
 */
@WebServlet("/QueryBooksServlet.do")
public class QueryBooksServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QueryBooksServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String name = request.getParameter("name");

		Book book = new Book();
		book.setCode(code);
		book.setName(name);
		BookService bookService = new BookServiceImpl();
		List<Book> lstBooks = bookService.queryBooksByCondition(book);
		// 将查询出的书籍数据保存到session中
		HttpSession session = request.getSession();
		session.setAttribute("lstBooks", lstBooks);
		// 将查询查询也保存到session
		session.setAttribute("queryCode", code);
		session.setAttribute("queryName", name);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
